<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12
 * Time: 10:47
 */

//------------------------
//考勤
//-------------------------

namespace app\attendance\controller;
use app\attendance\Base;
use com\gdl\kq\dataobject\publicarea\PublicAreaDTO;
use com\gdl\kq\interfaces\PublicAreaServiceClient;
use think\Config;
use think\Db;
use think\Exception;
use think\Loader;
date_default_timezone_set("PRC");
class AttAdminPublicArea extends Base
{
    //获取公共区域列表
	public function getPublicAreaList(){
        $PublicAreaId = $this->request->param('PublicAreaId') ? $this->request->param('PublicAreaId') : 0;
        $platformId = $this->request->param('platformId') ? $this->request->param('platformId') : 0;//平台ID
//        $adminuserid = $this->request->param('adminuserid') ? $this->request->param('adminuserid') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        if (empty($platformId)){
            $data = ['msg'=>'参数错误','code'=>400,'data'=>[]];
            return json_encode($data);
        }
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new PublicAreaServiceClient(NULL);
            \ApiClient::build($api);
            $data = [
                'adminReg'=>$adminReg,
//                'adminuserid'=>$adminuserid,
            ];
            $engprojectDo = new PublicAreaDTO();
            $engprojectDo->data	= $data;
            $engprojectDo->id	= $PublicAreaId;
            $engprojectDo->platformId	= $platformId;
            $result = $api->getPublicAreaList($engprojectDo);
            $plaform = [];
            if($result->code){
                /*****测试原型*****/
                $plaform = $result->data;
                foreach ($plaform as &$value){
                    $value['coordinate'] = unserialize($value['coordinate']);
                }
            }
            //checkstate 1是选中 0是未选中
            $data =  ['msg'=>'获取成功','code'=>200,'data'=>$plaform];
        } catch (Exception $ex) {
            $data = ['msg'=>'获取失败','code'=>400,'data'=>[]];
        }
        return json_encode($data);
	}

    //添加公共区域方案
    public function addPublicArea()
    {
        $title  = $this->request->param('title') ? $this->request->param('title') : '';
        $name  = $this->request->param('name') ? $this->request->param('name') : '';
//        $address  = $this->request->param('address') ? $this->request->param('address') : '';
        //['lng'=>'106.459223','lat'=>'29.509867']
        $coordinate       = $this->request->param('coordinate/a') ? $this->request->param('coordinate/a') : [];//经纬度
        $lng = $this->request->param('lng') ? $this->request->param('lng') : 0;//范围
        $lat = $this->request->param('lat') ? $this->request->param('lat') : 0;//范围
        $deviation = $this->request->param('deviation') ? $this->request->param('deviation') : 0;//范围
        $platformId = $this->request->param('platformId') ? $this->request->param('platformId') : 0;//平台ID
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        if (empty($platformId) || empty($name)){
            $data = ['msg'=>'参数错误','code'=>400,'data'=>[]];
            return json_encode($data);
        }
        if (empty($coordinate)){
            $coordinate = [
                'lng'=>(double)$lng,
                'lat'=>(double)$lat
            ];
        }
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new PublicAreaServiceClient(NULL);
            \ApiClient::build($api);
            $data = [
                'title'=>$title,
                'coordinate'=>serialize($coordinate),//经纬度['lng'=>'106.459223','lat'=>'29.509867']
                'platformId'=>$platformId,
                'deviation'=>$deviation,
//                'address'=>$address,
                'name'=>$name,
            ];
            $engprojectDo = new PublicAreaDTO();
            $engprojectDo->data	= $data;
            $engprojectDo->platformId	= $platformId;
            $result = $api->addPublicArea($engprojectDo);
            $plaform = [];
            if($result->code){
                /*****测试原型*****/
                $plaform = $result->data[0];
            }
            $data =  ['msg'=>$result->message,'code'=>200,'data'=>$plaform];
        } catch (Exception $ex) {
            $data = ['msg'=>'添加失败','code'=>400,'data'=>[]];
        }
        return json_encode($data);

    }

    /**
     * 编辑公共区域
     * @return string
     */
    public function editPublicArea()
    {
        $id = $this->request->param('id') ? $this->request->param('id') : 0;//公共区域ID
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $platformId = $this->request->param('platformId') ? $this->request->param('platformId') : 0;
        $coordinate = $this->request->param('coordinate/a') ? $this->request->param('coordinate/a') : [];
        //rules   [['type'=>'target_management','level'=>1],['type'=>'target_member','target_member'=>'manager3']]
        $unitid = $this->request->param('unitid') ? $this->request->param('unitid') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $title = $this->request->param('title') ? getStr($this->request->param('title')) : '';
        $address = $this->request->param('address') ? getStr($this->request->param('address')) : '';
        $deviation = $this->request->param('deviation') ? getStr($this->request->param('deviation')) : 0;
        if (empty($platformId) || empty($adminUserId) || empty($title) || empty($address)){
            $data = ['msg'=>'参数错误','code'=>400,'data'=>[]];
            return json_encode($data);
        }
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new PublicAreaServiceClient(NULL);
            \ApiClient::build($api);
            $PublicAreaDTO = new PublicAreaDTO();
            $PublicAreaDTO->id	= $id;
            $PublicAreaDTO->data	= [
                'title' =>$title,
                'address' =>$address,
                'deviation' =>$deviation,
                'platformId' =>$platformId,
                'coordinate' =>serialize($coordinate),
            ];
            $result = $api->editPublicArea($PublicAreaDTO);
            $pdata = [];
            if($result->code){
                $pdata = $result->data;
            }
            $data =  ['msg'=>$result->message,'code'=>200,'data'=>$pdata];
        } catch (Exception $ex) {
            $data = ['msg'=>'添加失败','code'=>400,'data'=>[]];
        }
        return json_encode($data);

    }
    /**
     * 删除公共区域
     * @return string
     */
    public function deletePublicArea()
    {
        $id = $this->request->param('id') ? $this->request->param('id') : 0;
        $adminReg = $this->request->param('adminReg') ? $this->request->param('adminReg') : 0;
        $platformId = $this->request->param('platformId') ? $this->request->param('platformId') : 0;
        $type = $this->request->param('type') ? $this->request->param('type') : 1;//1删除 单条 2删除所有
        if ((empty($id) && $type==1) || empty($adminReg)|| empty($platformId)){
            $data = ['msg'=>'参数错误','code'=>400,'data'=>[]];
            return json_encode($data);
        }

        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new PublicAreaServiceClient(NULL);
            \ApiClient::build($api);
            $PublicAreaDTO = new PublicAreaDTO();
            $PublicAreaDTO->id	= $id;
            $PublicAreaDTO->data	= ['platformId'=>$platformId,'type'=>$type];
            $result = $api->deletePublicArea($PublicAreaDTO);
            $pdata = [];
            if($result->code){
                $pdata = $result->data;
            }
            $data =  ['msg'=>$result->message,'code'=>200,'data'=>$pdata];
        } catch (Exception $ex) {
            $data = ['msg'=>'添加失败','code'=>400,'data'=>[]];
        }
        return json_encode($data);

    }
}
